package com.zang.blogz.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zang.blogz.constant.RedisConstant;
import com.zang.blogz.constant.ResultConstant;
import com.zang.blogz.enmus.FilePathEnum;
import com.zang.blogz.entity.Page;
import com.zang.blogz.exception.BizException;
import com.zang.blogz.mapper.PageMapper;
import com.zang.blogz.model.input.ro.PageImageRo;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.PageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zang.blogz.service.RedisService;
import com.zang.blogz.strategy.context.UploadStrategyContext;
import io.github.vampireachao.stream.core.optional.Opp;
import com.zang.blogz.utils.BeanCopyUtils;
import io.github.vampireachao.stream.core.optional.Sf;
import io.github.vampireachao.stream.plugin.mybatisplus.One;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


/**
 * <p>
 * 页面 服务实现类
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@Service
public class PageServiceImpl extends ServiceImpl<PageMapper, Page> implements PageService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Page> getPageInfoList() {

        List<Page> pageList;

        Object o = redisService.get(RedisConstant.PAGE_COVER);

        if (Opp.of(o).isEmpty()) {

            pageList = BeanCopyUtils.copyList(list(), Page.class);
            redisService.set(RedisConstant.PAGE_COVER, JSONUtil.toJsonStr(pageList));
        } else {

            pageList = JSONUtil.toList(o.toString(), Page.class);
        }

        return pageList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> deleteById(Integer id) {

        Page page = Opp.of(getById(id)).orElseThrow(() -> new RuntimeException("当前页面不存在无法删除"));

        removeById(page);

        redisService.del(RedisConstant.PAGE_COVER);

        return Result.ok(ResultConstant.DELETE_SUCCESS);
    }

    @Override
    public Result<String> uploadByPageId(PageImageRo pageImageRo) {

        AtomicReference<Page> page = new AtomicReference<>(null);

        Sf.of(pageImageRo.getId()).mayAlso(id -> page.set(One.of(Page::getId).eq(id).query()));

        if (Opp.of(page.get()).isEmpty()) {

            throw new BizException(ResultConstant.D_EXIST_DELETE_FAIL);
        }

        boolean b = update(Wrappers.lambdaUpdate(Page.class).set(Page::getPageCover, pageImageRo.getMultipartFile()).eq(Page::getId, pageImageRo.getId()));


        if (b){

            redisService.delVague(RedisConstant.PAGE_COVER);
            return Result.ok(ResultConstant.UPDATE_SUCCESS);
        }
        return Result.error(ResultConstant.UPDATE_FAIL);

    }
}
